package com.weixin.common;


import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.render.ViewType;
import com.weixin.controller.AdminController;
import com.weixin.controller.CityController;
import com.weixin.controller.CodeController;
import com.weixin.controller.ProvinceController;
import com.weixin.controller.StoreController;
import com.weixin.controller.UserController;
import com.weixin.dao.Admin;
import com.weixin.dao.City;
import com.weixin.dao.Code;
import com.weixin.dao.Province;
import com.weixin.dao.Store;
import com.weixin.dao.Use;

/**
 * API引导式配置
 */
public class Config extends JFinalConfig {
	
	/**
	 * 配置常量
	 */
	public void configConstant(Constants me) {
		// 加载少量必要配置，随后可用getProperty(...)获取值
		loadPropertyFile("a_little_config.txt");
		me.setDevMode(getPropertyToBoolean("devMode", false));
//		me.setBaseViewPath("/view");
	}
	
	/**
	 * 配置路由
	 */
	public void configRoute(Routes me) {
		me.add("/user",UserController.class,"/");// 第三个参数省略时默认与第一个参数值相同，在此即为 "/blog"
		me.add("/city", CityController.class,"/");
		me.add("/province",ProvinceController.class,"/static");
		me.add("/code", CodeController.class,"/");
		me.add("/store", StoreController.class,"/");
		me.add("/admin", AdminController.class,"/");
	}
	
	/**
	 * 配置插件
	 */
	public void configPlugin(Plugins me) {
		// 配置C3p0数据库连接池插件
		C3p0Plugin c3p0Plugin = new C3p0Plugin(getProperty("jdbcUrl").trim(), getProperty("user").trim(), getProperty("password").trim());
		me.add(c3p0Plugin);
		
		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
		me.add(arp);
		arp.addMapping("users", Use.class);	// 映射blog 表到 Blog模型
		arp.addMapping("city", City.class);
		arp.addMapping("code", Code.class);
		arp.addMapping("province", Province.class);
		arp.addMapping("store", Store.class);
		arp.addMapping("admin", Admin.class);
		
	}
	
	/**
	 * 配置全局拦截器
	 */
	public void configInterceptor(Interceptors me) {
		
	}
	
	/**
	 * 配置处理器
	 */
	public void configHandler(Handlers me) {
		me.add(new ContextPathHandler("contextPath"));//设置上下文路径  防止样式丢失
	}
	
	/**
	 * 建议使用 JFinal 手册推荐的方式启动项目
	 * 运行此 main 方法可以启动项目，此main方法可以放置在任意的Class类定义中，不一定要放于此
	 */
	public static void main(String[] args) {
		JFinal.start("WebRoot", 80, "/", 5);
	}
}
